package org.lfh.wintersun.web.user.controller;

import org.lfh.wintersun.common.base.PageUtils;
import org.lfh.wintersun.common.result.CommonResult;
import org.lfh.wintersun.web.user.entity.SysUserEntity;
import org.lfh.wintersun.web.user.dto.SysUserDTO;
import org.lfh.wintersun.web.user.service.SysUserService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

import java.util.Arrays;
import java.util.Collections;

/**
 * 用户信息(SysUser)表控制层
 *
 * @author lfh
 * @since 2024-10-10 15:35:09
 */
@RestController
@RequestMapping("/user/sysUser")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 分页查询
     *
     * @param sysUserDTO 参数
     * @return 查询结果
     */
    @GetMapping("/queryByPage")
    public CommonResult<PageUtils<SysUserEntity>> queryByPage(SysUserDTO sysUserDTO) {
        return CommonResult.success(sysUserService.queryByPage(sysUserDTO));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/info/{id}")
    public CommonResult<SysUserEntity> queryById(@PathVariable("id") Long id) {
        return CommonResult.success(sysUserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysUserEntity 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public CommonResult<SysUserEntity> save(@RequestBody SysUserEntity sysUserEntity) {
        return CommonResult.success(sysUserService.insert(sysUserEntity));
    }

    /**
     * 编辑数据
     *
     * @param sysUserEntity 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public CommonResult<SysUserEntity> edit(@RequestBody SysUserEntity sysUserEntity) {
        return CommonResult.success(sysUserService.update(sysUserEntity));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/deleteById")
    public CommonResult<?> deleteById(@RequestBody Long id) {
        this.sysUserService.deleteById(id);
        return CommonResult.success();
    }

    /**
     * 删除数据
     *
     * @param ids 主键集合
     * @return 删除是否成功
     */
    @PostMapping("/removeByIds")
    public CommonResult<?> removeByIds(@RequestBody Long[] ids) {
        this.sysUserService.removeByIds(Collections.singletonList(ids));
        return CommonResult.success();
    }

}

